package com.njust.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.njust.domain.dto.MessageDTO;
import com.njust.domain.dto.MessagePageQueryDTO;
import com.njust.domain.po.Message;
import com.njust.domain.po.User;
import com.njust.domain.vo.PageQueryVO;
import com.njust.exception.AccountAlreadyExists;
import com.njust.exception.AddMessageError;
import com.njust.mapper.MessageMapper;
import com.njust.service.MessageService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Author Imak
 * @Date 2024/11/8 15:56
 * @留言service
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    /**
     * 新增留言
     * @param messageDTO
     */
    public void addMessage(MessageDTO messageDTO){
        String content = messageDTO.getContent();
        Date created_time=messageDTO.getCreated_time();
        Message message=Message.builder()
                .content(content)
                .created_time(created_time)
                .build();
        int code=messageMapper.insert(message);
        if(code!=1){
            throw new AddMessageError("留言操作失败!");
        }
    }

    /**
     * 分页查询留言
     * @param messagePageQueryDTO
     * @return
     */
    public PageQueryVO pageQuery(MessagePageQueryDTO messagePageQueryDTO) {
        PageHelper.startPage(messagePageQueryDTO.getPage(), messagePageQueryDTO.getPageSize());

        Page<Message> page=messageMapper.pageQuery(messagePageQueryDTO);
        long total=page.getTotal();
        List<Message> records=page.getResult();

        return new PageQueryVO(total,records);
    }
}
